using System;
using System.Diagnostics;
using AzureContrib.WindowsAzure.Properties;
using AzureContrib.WindowsAzure.Utils;

namespace AzureContrib.WindowsAzure.Diagnostics
{
	/// <summary>
	/// Extensions on <see cref="TraceLevel"/>
	/// </summary>
	public static class TraceMessageExtensions
	{
		/// <summary>
		/// Convert a <see cref="string"/> to a <see cref="TraceLevel"/>.
		/// </summary>
		/// <param name="traceLevelString">The <see cref="string"/> to convert.</param>
		/// <returns>The parsed <see cref="TraceLevel"/>.</returns>
		public static TraceLevel ParseTraceLevel(this string traceLevelString)
		{
			TraceLevel traceLevel;
			if (!TryParseTraceLevel(traceLevelString, out traceLevel))
			{
				throw new FormatException(Strings.NotATraceLevel.F(traceLevelString));
			}

			return traceLevel;
		}

		/// <summary>
		/// Try convert a <see cref="string"/> to a <see cref="TraceLevel"/>.
		/// </summary>
		/// <param name="traceLevelString">The <see cref="string"/> to convert.</param>
		/// <param name="traceLevel">The parsed <see cref="TraceLevel"/>.</param>
		/// <returns>True if the <paramref name="traceLevelString"/> was a <see cref="TraceLevel"/>.</returns>
		public static bool TryParseTraceLevel(this string traceLevelString, out TraceLevel traceLevel)
		{
			traceLevel = TraceLevel.Off;
			return !string.IsNullOrEmpty(traceLevelString) && 
			       Enum.TryParse(traceLevelString, out traceLevel);
		}
	}
}